import BaseView from "../../../cfw/mvc/BaseView";
import RedPacketC from "../RedPacketC";
import BaseChannel from "../../base/BaseChannel";
import GameItemModel from "../../base/GameItemModel";
import RandomHelper from "../../../cfw/tools/RandomHelper";
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import { InitValue } from "../../../public/Config";
import BagManager from "../../../game/bag/BagManager";
import BigExportC from "../../appBoxAd/BigExportC";
import ModuleManager from "../../../cfw/module/ModuleManager";
import { ResType } from "../../../cfw/res/ResInterface";
import ResItem from "../../../cfw/res/ResItem";
import SDKManager from "../../SDKManager";


const { ccclass, property } = cc._decorator;

@ccclass
export default class RedPacketView extends BaseView {

	@property({ type: cc.Button, displayName: "back$VButton" })
	back$VButton: cc.Button = null;

	@property({ type: cc.Button, displayName: "conform$VButton" })
	conform$VButton: cc.Button = null;

	@property({ type: cc.Label, displayName: "desc$VLabel" })
	desc$VLabel: cc.Label = null;

	@property({ type: cc.Label, displayName: "name$VLabel" })
	name$VLabel: cc.Label = null;

	@property({ type: cc.Sprite, displayName: "icon$VSprite" })
	icon$VSprite: cc.Sprite = null;


	protected controller: RedPacketC;
	protected model: BaseChannel;
	protected item: GameItemModel;
	onLoad() {
		let list = this.model.getAdData()
		this.item = list[RandomHelper.random(0, list.length)]
		GlobalEvent.instance().on(GlobalEvent.EVENT_SHOW, this.eventShow, this)
		if (this.item) {
			this.name$VLabel.string = this.item.getGameName()
			this.desc$VLabel.string = '进入' + this.item.getGameName() + '领取红包'
			ModuleManager.publicLoader().loadRes(this.item.getGameIcon(), ResType.Remote, (err, tex: ResItem) => {
				if (err || !cc.isValid(this.node)) {
					return;
				}
				this.icon$VSprite.spriteFrame = new cc.SpriteFrame(tex.getRes())
			})
		}
	}




	onDestroy() {
		super.onDestroy()
		GlobalEvent.instance().off(GlobalEvent.EVENT_SHOW, this.eventShow, this)
	}

	eventShow() {
		BagManager.addCoin(InitValue.shareNum)
	}

	onback$VButtonClick() {
		this.hide()
		BigExportC.instance().intoExport1(() => {
			BigExportC.instance().intoExport3()
		})
	}

	onconform$VButtonClick() {
		if (this.item) {
			SDKManager.getChannel().navigate2Mini(this.item, (
			) => {
				this.hide()
			})
		}

	}



}